#include"../../src/server.hpp"
#include <iostream>
#include <cassert>
#include <cstring>

void test_basic_write_read() {
    std::cout << "=== 测试基本读写功能 ===" << std::endl;
    Buffer b;
    
    // 测试写入和读取
    const char* str = "hello ";
    b.write(str, strlen(str));
    b.writeString(std::string("world!"));
    
    // 检查可读数据大小
    std::cout << "可读数据大小: " << b.readAbleSize() << std::endl;
    assert(b.readAbleSize() == 12); // "hello world!"
    
    // 读取所有数据
    char buf[13] = {0};
    b.read(buf, 12);
    std::cout << "读取到的数据: " << buf << std::endl;
    assert(strcmp(buf, "hello world!") == 0);
    
    std::cout << "✓ 基本读写测试通过\n" << std::endl;
}

void test_expansion() {
    std::cout << "=== 测试缓冲区扩容 ===" << std::endl;
    Buffer b;
    
    // 写入大量数据触发扩容
    std::string large_data(2000, 'A'); // 2000个A，超过默认1024大小
    b.writeString(large_data);
    
    std::cout << "写入大数据后可读大小: " << b.readAbleSize() << std::endl;
    assert(b.readAbleSize() == 2000);
    
    // 读取部分数据验证
    std::string result = b.readAsString(10);
    std::cout << "读取前10个字符: " << result << std::endl;
    assert(result == "AAAAAAAAAA");
    
    std::cout << "✓ 扩容测试通过\n" << std::endl;
}

void test_data_movement() {
    std::cout << "=== 测试数据移动 ===" << std::endl;
    Buffer b;
    
    // 先写入一些数据
    b.writeString(std::string("first_data"));
    
    // 读取一部分，腾出前面空间
    char temp[5];
    b.read(temp, 5); // 读取"first"
    
    std::cout << "读取部分数据后，剩余可读: " << b.readAbleSize() << std::endl;
    std::cout << "前置空间: " << b.frontSize() << std::endl;
    std::cout << "后置空间: " << b.tailSize() << std::endl;
    
    // 写入较大数据，触发数据移动
    std::string large_data(1015, 'B');
    b.writeString(large_data);
    
    std::cout << "数据移动后，可读数据大小: " << b.readAbleSize() << std::endl;
    
    // 验证剩余的原数据还在
    std::string remaining = b.readAsString(5);
    std::cout << "原数据剩余部分: " << remaining << std::endl;
    assert(remaining == "_data");
    
    std::cout << "✓ 数据移动测试通过\n" << std::endl;
}

void test_http_line_reading() {
    std::cout << "=== 测试HTTP行读取 ===" << std::endl;
    Buffer b;
    
    // 写入多行HTTP数据
    std::string http_data = "GET /index.html HTTP/1.1\n"
                           "Host: example.com\n"
                           "User-Agent: TestAgent\n"
                           "\n";
    b.writeString(http_data);
    
    // 逐行读取
    std::string line1 = b.getLine();
    std::string line2 = b.getLine();
    std::string line3 = b.getLine();
    std::string line4 = b.getLine();
    
    std::cout << "第1行: " << line1;
    std::cout << "第2行: " << line2;  
    std::cout << "第3行: " << line3;
    std::cout << "第4行: " << line4;
    
    assert(line1 == "GET /index.html HTTP/1.1\n");
    assert(line2 == "Host: example.com\n");
    assert(line3 == "User-Agent: TestAgent\n");
    assert(line4 == "\n");
    
    std::cout << "✓ HTTP行读取测试通过\n" << std::endl;
}

void test_buffer_to_buffer() {
    std::cout << "=== 测试Buffer间拷贝 ===" << std::endl;
    Buffer b1, b2;
    
    // 在b1中写入数据
    b1.writeString(std::string("source_buffer_data"));
    
    // 将b1的数据写入b2
    b2.writeBuffer(b1);
    
    std::cout << "b1可读数据: " << b1.readAbleSize() << std::endl;
    std::cout << "b2可读数据: " << b2.readAbleSize() << std::endl;
    
    // 验证数据正确性
    std::string data1 = b1.readAsString(b1.readAbleSize());
    std::string data2 = b2.readAsString(b2.readAbleSize());
    
    std::cout << "b1数据: " << data1 << std::endl;
    std::cout << "b2数据: " << data2 << std::endl;
    
    assert(data1 == data2);
    assert(data1 == "source_buffer_data");
    
    std::cout << "✓ Buffer拷贝测试通过\n" << std::endl;
}

void test_clear_function() {
    std::cout << "=== 测试清空功能 ===" << std::endl;
    Buffer b;
    
    // 写入数据
    b.writeString(std::string("test_data"));
    std::cout << "清空前可读数据: " << b.readAbleSize() << std::endl;
    assert(b.readAbleSize() > 0);
    
    // 清空
    b.clear();
    std::cout << "清空后可读数据: " << b.readAbleSize() << std::endl;
    assert(b.readAbleSize() == 0);
    
    // 清空后应该可以正常写入
    b.writeString(std::string("new_data"));
    assert(b.readAbleSize() == 8);
    
    std::cout << "✓ 清空功能测试通过\n" << std::endl;
}

void run_all_tests() {
    std::cout << "开始Buffer类全面测试...\n" << std::endl;
    
    test_basic_write_read();
    test_expansion();
    test_data_movement();
    test_http_line_reading();
    test_buffer_to_buffer();
    test_clear_function();
    
    std::cout << "🎉 所有测试通过！Buffer类工作正常！" << std::endl;
}

int main()
{
    try {
        run_all_tests();
    } catch (const std::exception& e) {
        std::cout << "❌ 测试失败: " << e.what() << std::endl;
        return -1;
    }
    
    return 0;
}